package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.exception.BusinessExpetion;
import com.qk.mapper.UserMapper;
import com.qk.service.UserService;
import com.qk.utils.JwtUtils;
import com.qk.vo.LoginResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public PageResult<User> list(UserDto userDto) {
//        1.设置分页参数
        PageHelper.startPage(userDto.getPage(),userDto.getPageSize());
//        2.调用Mapper接口查询
         List<User> users=userMapper.selectList(userDto);
//        3.解析并封装结果
        Page<User> p = (Page<User>) users;
        return new PageResult<>(p.getTotal(),p.getResult());
    }

    @Override
    public void add(User user) {
//        补全属性
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
//        给密码设置默认值并且加密
         String password=user.getUsername()+"123";
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
//        调用Mapper方法
        userMapper.insert(user);
    }

    @Override
    public void deleteById(List<Integer> ids) {
        userMapper.deleteById(ids);
    }

    @Override
    public User getUserById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public void update(User user) {
//补全属性
        user.setUpdateTime(LocalDateTime.now());
//        调用Mapper方法
        userMapper.update(user);
    }

//    根据角色标识查询用户


    @Override
    public List<User> selectByRoleLabel(String label) {
        return  userMapper.selectByRoleLabel(label);
    }



//    登录

    @Override
    public LoginResultVo login(User user) {
//        1.调用Mapper接口查询用户 u是数据库的用户
        User u = userMapper.selectByUsername(user.getUsername());
//        2.判断用户是否存在
        if(u==null){
            log.info("用户不存在");
//            return null;
            throw new BusinessExpetion("用户名或密码错误");
        }
//        3.判断密码是否正确
        String pass = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        if(!pass.equals(u.getPassword())){
            log.info("密码错误");
//            return null;
            throw new BusinessExpetion("用户名或密码错误");
        }
//        4.判断状态是否正常
        if(u.getStatus()==0){
            log.info("用户被禁用");
//            return null;
            throw new BusinessExpetion("用户被禁用");
        }
//        5.封装结果
        LoginResultVo loginResultVo = new LoginResultVo();
        loginResultVo.setId(u.getId());
        loginResultVo.setUsername(u.getUsername());
        loginResultVo.setName(u.getName());
        loginResultVo.setImage(u.getImage());
        loginResultVo.setRoleLabel(u.getRoleLabel());
//        调用工具类生成jwt令牌
        Map<String,Object> claims = new HashMap<>();
        claims.put("id",u.getId());
        claims.put("username",u.getUsername());
        String jwt = JwtUtils.generateToken(claims);

        loginResultVo.setToken(jwt);

        return loginResultVo;
    }

//    查询所有用户

    @Override
    public List<User> selectAll() {
        return userMapper.selectAll();
    }
//    根据部门id查询用户

    @Override
    public List<User> selectByDeptId(Integer deptId) {
        return userMapper.selectByDeptId(deptId);
    }
}
